package server;

import java.util.Random;

/**
 * A helper object for the {@link ScheduleProblem} class.
 * ScheduleConflicts is used to determine the fitness of a given
 * {@link ScheduleIndividual} gene.   
 * <p>
 *@author Kathryn Glowinski
 */
public class ScheduleConflicts {

	int [][] conflicts;
	int numClasses;
	
	/**
	 *Constructor for the ScheduleConflicts class. Takes in @param 
	 *numberOfClasses, which will be used to determine the size of the
	 *@param conflicts 2D array.
	 *<p>
	 *@param numberOfClasses	An int specifying the number of classes that
	 *							should be factored into the conflicts matrix. 
	 *							The more classes present, the more complex the 
	 *							gene will be.
	 */
	public ScheduleConflicts(int numberOfClasses){
		
		numClasses = numberOfClasses;
		conflicts = new int [numClasses][numClasses];
	}
	
	/**
	 * A function to randomly assign values of 0 or 1 to each of the potential
	 * interactions between classes. 0 indicates that there is no conflict
	 * between two classes, and 1 indicates that there is a conflict. The randomness
	 * scalles with the "difficulty level" provided by @param difficulty.  
	 * <p>
	 * @param difficulty	Double between 0 and 1 that helps determine whether a class
	 * 						is in conflict with another classes. If a randomly
	 * 						generated number if below this difficulty threshold,
	 * 						we determine that there is a conflict, and if it is
	 * 						above, there is not. The higher difficulty is, the larger number
	 * 						of conflicts that will appear, and the lower a gene's fitness is
	 * 						likely to be.   
	 */
	void generateRandomly(double difficulty){
		
		Random rand = new Random();
		
		for (int i = 0; i < conflicts.length; i++){
			for (int j = 0; j < conflicts[i].length; j++){
				
				double random = rand.nextDouble();
				
				//If the class is the same, we want there to be no conflict.
				if (i == j){
					conflicts[i][j] = 0;
				}
				else{
					if (random < difficulty){
						conflicts[i][j] = 1;
                        conflicts[j][i] = 1;
					} else {
						conflicts[i][j] = 0;
                        conflicts[j][i] = 0;
					}
				}
			}
		}
	}
	
}
